﻿
// PenBrushView.cpp: CPenBrushView 类的实现
//
#include<math.h>
#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "PenBrush.h"
#endif

#include "PenBrushDoc.h"
#include "PenBrushView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CPenBrushView

IMPLEMENT_DYNCREATE(CPenBrushView, CView)

BEGIN_MESSAGE_MAP(CPenBrushView, CView)
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CPenBrushView::OnFilePrintPreview)
	ON_WM_CONTEXTMENU()
	ON_WM_RBUTTONUP()
END_MESSAGE_MAP()

// CPenBrushView 构造/析构

CPenBrushView::CPenBrushView() noexcept
{
	// TODO: 在此处添加构造代码

}

CPenBrushView::~CPenBrushView()
{
}

BOOL CPenBrushView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CView::PreCreateWindow(cs);
}

// CPenBrushView 绘图

void CPenBrushView::OnDraw(CDC* pDC)
{
	CPenBrushDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: 在此处为本机数据添加绘制代码
	CPen* pNewPen;
	CPen* pOldPen = nullptr;
	CBrush* pNewBrush;
	CBrush* pOldBrush;
	// 画笔的使用
	pNewPen = new CPen;
	if (pNewPen->CreatePen(PS_DASHDOT, 3, RGB(255, 0, 0)))
	{
		pOldPen = pDC->SelectObject(pNewPen);
		// 用新创建的画笔绘图
		pDC->MoveTo(10, 10);
		pDC->LineTo(150, 10);
		// 恢复设备描述表中的原有画笔
		pDC->SelectObject(pOldPen);
	}
	else
	{
		AfxMessageBox(_T("CreatePen Error!"));	// 给出错误提示
	}
	delete pNewPen;	// 删除新画笔
	// 使用堆画笔绘制一矩形
	pDC->SelectStockObject(BLACK_PEN);
	pDC->MoveTo(10, 20);
	pDC->LineTo(200, 20);
	pDC->LineTo(200, 40);
	pDC->LineTo(10, 40);
	pDC->LineTo(10, 20);
	// 恢复DC
	pDC->SelectObject(pOldPen);
	// 画刷的使用
	pNewBrush = new CBrush;
	if (pNewBrush->CreateSolidBrush(RGB(180, 70, 230)))
	{
		pOldBrush = pDC->SelectObject(pNewBrush);	// 选择新画刷
		pDC->Rectangle(10, 60, 200, 100);		// 绘制矩形
		pDC->SelectObject(pOldBrush);		// 恢复设备描述表中原有的画刷
	}
	delete pNewBrush;						// 删除新画刷
	pNewBrush = new CBrush;
	if (pNewBrush->CreateHatchBrush(HS_DIAGCROSS, RGB(250, 150, 230)))
	{
		pOldBrush = pDC->SelectObject(pNewBrush);	// 选择新画刷
		pDC->Rectangle(10, 120, 200, 200);		// 绘制矩形
		pDC->SelectObject(pOldBrush);		// 恢复设备描述表中原有的画刷
	}
	delete pNewBrush;	// 删除新画刷

}


// CPenBrushView 打印


void CPenBrushView::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
	AFXPrintPreview(this);
#endif
}

BOOL CPenBrushView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// 默认准备
	return DoPreparePrinting(pInfo);
}

void CPenBrushView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加额外的打印前进行的初始化过程
}

void CPenBrushView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加打印后进行的清理过程
}

void CPenBrushView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void CPenBrushView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}


// CPenBrushView 诊断

#ifdef _DEBUG
void CPenBrushView::AssertValid() const
{
	CView::AssertValid();
}

void CPenBrushView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CPenBrushDoc* CPenBrushView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPenBrushDoc)));
	return (CPenBrushDoc*)m_pDocument;
}
#endif //_DEBUG


// CPenBrushView 消息处理程序
